#include <iostream>
//嵌套类+静态对象来控制单例对象的释放
using namespace std;
class Singleton{
    public:
    static Singleton * getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Singleton();
        }
        return _pInstance;
    }
    class AutoRelease{
       public:
       AutoRelease()
       {}
       ~AutoRelease(){
        cout << "~AutoRelease" << endl;
          if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
          }
       }
    };
    private:
    static Singleton * _pInstance;
    static AutoRelease _ar;
};
Singleton * Singleton:: _pInstance = nullptr;
Singleton::AutoRelease Singleton:: _ar;
void test(){
    Singleton::getInstance();
}
int main()
{
    test();
    return 0;
}

